package com.ruoyi.web.controller.common;

import com.alibaba.fastjson2.JSON;
import com.baidu.ueditor.ActionEnter;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.file.FileUploadUtils;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@RestController
public class UeditorController {

    //配置ueditor后端上传接口的验证
    @Anonymous
    @RequestMapping("/config")
    public String config(HttpServletRequest request, HttpServletResponse response, String action, MultipartFile[] upfile) throws IOException {
        if (action.equals("config")) {
            request.setCharacterEncoding("utf-8");
            response.setHeader("Content-Type", "text/html");
            String path = ClassUtils.getDefaultClassLoader().getResource("").getPath() + "config";
            System.out.println("path = " + path);
            PrintWriter printWriter = response.getWriter();
            printWriter.write(new ActionEnter(request, path).exec());
            printWriter.flush();
            printWriter.close();
        } else if (action.equals("uploadimage")) {
            Map<String, Object> result = new HashMap<String, Object>();
            for (MultipartFile multipartFile : upfile) {
                String upload = FileUploadUtils.upload(RuoYiConfig.getUploadPath(),multipartFile);
                System.out.println("upload = " + upload);
                System.out.println("upload.substring(upload.lastIndexOf(\"/\")) = " + upload.substring(upload.lastIndexOf("/")));
                result.put("title", upload.substring(upload.lastIndexOf("/")+1));
                result.put("original", multipartFile.getOriginalFilename());
                result.put("state", "SUCCESS");
                result.put("url", upload);
                String jStr = JSON.toJSONString(result);
                return jStr;
            }
            return null;
        }else if(action.equals("uploadvideo")){
            Map<String, Object> result = new HashMap<String, Object>();
            for (MultipartFile multipartFile : upfile) {
                String upload = FileUploadUtils.upload(RuoYiConfig.getUploadPath(),multipartFile);
                System.out.println("upload = " + upload);
                System.out.println("upload.substring(upload.lastIndexOf(\"/\")) = " + upload.substring(upload.lastIndexOf("/")));
                result.put("title", upload.substring(upload.lastIndexOf("/")+1));
                result.put("original", multipartFile.getOriginalFilename());
                result.put("state", "SUCCESS");
                result.put("url", upload);
                String jStr = JSON.toJSONString(result);
                return jStr;
            }
        }else{
            Map<String, Object> result = new HashMap<String, Object>();
            for (MultipartFile multipartFile : upfile) {
                String upload = FileUploadUtils.upload(RuoYiConfig.getUploadPath(),multipartFile);
                System.out.println("upload = " + upload);
                System.out.println("upload.substring(upload.lastIndexOf(\"/\")) = " + upload.substring(upload.lastIndexOf("/")));
                result.put("title", upload.substring(upload.lastIndexOf("/")+1));
                result.put("original", multipartFile.getOriginalFilename());
                result.put("state", "SUCCESS");
                result.put("url", upload);
                String jStr = JSON.toJSONString(result);
                return jStr;
            }
        }
        return null;
    }

}
